package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 由cid查询组
     *
     * @param cid cid
     * @return {@link List}<{@link SpecGroup}>
     */
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return specGroupMapper.select(specGroup);
    }


    /**
     * 通过组查询参数列表
     *
     * @param aLong
     * @param gid       gid
     * @param generic
     * @param searching
     * @return {@link List}<{@link SpecParam}>
     */
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam specParam = new SpecParam();

        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setGeneric(generic);
        specParam.setSearching(searching);
        //这四个字段若为空则不参与查询扩展性更好
        return specParamMapper.select(specParam);
    }

    /*  查询参数组 */
    public List<SpecGroup> queryGroupsWithPar(Long cid) {
        List<SpecGroup> groups = this.queryGroupsByCid(cid);
        groups.forEach(
                group -> {
                    List<SpecParam> params = this.queryParams(group.getId(), null, null, null);
                    group.setParams(params);
                });
        return groups;
    }
}
